package com.hua.common.commonview;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;

import com.hua.common.commontools.DipUtil;
/**
 * 进度条控件
 * @author huaxiaolin_pc
 *
 */
public class ProgressControl extends LinearLayout {

	private int Mwidth = 0;
	/**
	 * 绘制的圆点画笔
	 */
	private Paint pointPaint = null;
	/**
	 * 绘制圆环的画笔
	 */
	private Paint arcPaint = null;

	private float point = 0f;
	private float count = 0f;
	private AnimatorSet animatorSet = null;
	/**
	 * 外圆的画笔粗细
	 */
	private float ringOutPaintWidth = 0f;
	/**
	 * 圆环距离边界的距离
	 */
	private float curryRadius=0f;
	public ProgressControl(Context context) {
		super(context);
		init();
	}

	public ProgressControl(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ProgressControl(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		this.initPaint();
		this.initAnim();
	}

	/**
	 * 初始化画笔
	 */
	private void initPaint() {
		this.ringOutPaintWidth= DipUtil.Dp2px(getContext(), 2);
		this.curryRadius=DipUtil.Dp2px(getContext(), 8);
		pointPaint = new Paint();
		pointPaint.setAntiAlias(true);
		pointPaint.setStyle(Style.FILL);
		pointPaint.setStrokeWidth(25);
		pointPaint.setAntiAlias(true); // 消除锯齿
		pointPaint.setDither(true);
		pointPaint.setStrokeCap(Cap.ROUND);
		pointPaint.setColor(Color.parseColor("#ff0085cf"));
		arcPaint = new Paint();
		arcPaint.setAntiAlias(true);
		arcPaint.setStyle(Style.STROKE);
		arcPaint.setStrokeWidth(this.ringOutPaintWidth);
		arcPaint.setAntiAlias(true); // 消除锯齿
		arcPaint.setDither(true);
		arcPaint.setStrokeCap(Cap.ROUND);
		arcPaint.setColor(Color.parseColor("#ff0085cf"));
	}

	/**
	 * 初始化动画
	 */
	private void initAnim() {
		ValueAnimator pointAnimator = ValueAnimator.ofFloat(0, 360f);
		pointAnimator.setRepeatCount(Integer.MAX_VALUE);
		pointAnimator.setRepeatMode(ValueAnimator.RESTART);
		pointAnimator
				.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
					@Override
					public void onAnimationUpdate(ValueAnimator animation) {
						point = (float) animation.getAnimatedValue();
						drawArc();
					}

				});
		pointAnimator.setInterpolator(new LinearInterpolator());

		ValueAnimator countAnimator = ValueAnimator.ofFloat(0, 360f);
		countAnimator.setRepeatCount(Integer.MAX_VALUE);
		countAnimator.setRepeatMode(ValueAnimator.RESTART);
		countAnimator
				.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
					@Override
					public void onAnimationUpdate(ValueAnimator animation) {
						count = (float) animation.getAnimatedValue();
						drawArc();
					}

				});
		countAnimator.setInterpolator(new LinearInterpolator());

		this.animatorSet = new AnimatorSet();
		this.animatorSet.playTogether(pointAnimator, countAnimator);
		this.animatorSet.setDuration(1500);

	}

	private void drawArc() {
		this.invalidate();
	}

	public void start() {
		if (animatorSet == null) {
			this.initAnim();
		}
		this.animatorSet.start();
	}

	/**
	 * 绘制圆点
	 */
	private void drawPoint(Canvas canvas) {
		float temp=this.curryRadius*2;
		RectF oval1 = new RectF(temp, temp, Mwidth - temp, Mwidth - temp);
		canvas.drawArc(oval1, 0, 360, false, pointPaint);// 小弧形
	}

	/**
	 * 绘制圆弧
	 *
	 * @param canvas
	 */
	private void drawArc(Canvas canvas) {
		RectF oval1 = new RectF(curryRadius, curryRadius, Mwidth - curryRadius, Mwidth - curryRadius);
		canvas.drawArc(oval1, point, count, false, arcPaint);// 小弧形
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawPoint(canvas);
		drawArc(canvas);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		this.initmWidth();
	}

	private void initmWidth() {
		this.Mwidth = getMeasuredWidth();
	}
}
